const App = getApp();
const wxParse = require("../../../wxParse/wxParse.js");
Page({
  data: {
    floorstatus: false, // 返回顶部
    detail: {}, // 商品详情信息
  },
  onLoad(e) {
    let _this = this,token = wx.getStorageSync('ACCESS_TOKEN')
      if(token){
        _this.setData({token:token})
        _this.data.goods_id = e.goods_id
        wx.showLoading({title: '加载中',mask:true})
        _this.getGoodsDetail()
        _this.getIntegral()
        wx.hideLoading()
      }else {
        App.args.page.onLoad(this, e)
      }
  },
  getGoodsDetail() {
    let _this = this
    App.args.request({
      url: App.args.api.siteinfo.sitelive + 'integral/goodsDetail',
      data:{
          storeId: App.args.api.siteinfo.hosId2,
          goods_id:_this.data.goods_id
      },
      success(res){
        console.log(res)
        wxParse.wxParse('content', 'html', res.content, _this, 0)
        _this.setData({detail: res})
        console.log(_this.data.detail)
      }
    })
  },
  getIntegral: function(t) {
    let _this = this
    App.args.request({
        url: App.args.api.siteinfo.sitelive + 'integral/info',
        data:{
            storeId: App.args.api.siteinfo.hosId2,
            //openId:_this.data.token
            openId:'oSdH80GceCJqgJ-RVAIo_ETdQeoU'
        },
        success(res){
            res.code == 0 && _this.setData({integral: res.jf})
        }
    })
},
  exchange: function(t) {
    let _this = this, info = t.currentTarget.dataset.index
    _this.data.integral < info.integral ? App.showError('当前积分不足') : wx.showModal({
        title: '提示',
        content: '是否兑换',
        success (re) {
            re.confirm && wx.request({
                header: {'content-type': 'application/json'},
                url: App.args.api.siteinfo.sitelive + 'integral/exchange',
                data:{
                    storeId: App.args.api.siteinfo.hosId2,
                    //openId:_this.data.token,
                    openId:'oSdH80GceCJqgJ-RVAIo_ETdQeoU',
                    goodsId:info.id
                },
                success(res){
                  console.log(res)
                    if(res.data.code == 0) {
                        _this.setData({integral:_this.data.integral-info.integral})
                        wx.showModal({
                            title: '兑换成功',
                            content: info.name,
                            cancelText:'确认',
                            confirmText:'立即使用',
                            success (r) {
                              wx.navigateTo({
                                url: r.confirm ? "/pages/integral-mall/order/index" : "/pages/integral-mall/index/index"
                              })
                            }
                        })
                    }else{
                      App.showError(res.data.msg)
                    }
                }
            })
        }
    })
  },
  onScrollTop(t) {
    let _this = this;
    _this.setData({
      scrollTop: 0
    });
  },
  scroll(e) {
    let _this = this;
    _this.setData({
      floorstatus: e.detail.scrollTop > 200
    })
  },
  onShareAppMessage() {
    const _this = this
    const params = App.getShareUrlParams({
      'goods_id': _this.data.goods_id
    })
    return {
      title: _this.data.detail.name,
      path: "/pages/integral-mall/goods-info/index?" + params
    }
  },
})